/**
 * Created by PhpStorm.
 * 调用手机APK原生页面
 * User: wuzhentao
 * Date: 15-1-14
 * Time: 上午10:05
 */

/**
 * 初始化驱动
 */
if(window.openapk){
    //android
    window.openapk.callHandler = function(n,p,callback){
        /**
         * 安卓用json作参数
         * @type {string}
         */
        var str = "{";
        for ( var o in p) {
            str += o + ":" + p[o] + ",";
        }
        if (str.substr(str.length - 1) == ",")
            str = str.substr(0, str.length - 1);
        str += "}";
        var result = this[n](str);
        callback(result);
    }
}else{
    //iOS
    connectWebViewJavascriptBridge(function(bridge) {
        /**
         * 日志ID
         * @type {number}
         */
        var uniqueId = 1;

        /**
         * 日志
         * @param message
         * @param data
         */
        function log(message, data) {
            var log = document.getElementById('log');
            var el = document.createElement('div');
            el.className = 'logLine';
            el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data);
            if (log.children.length) {
                log.insertBefore(el, log.children[0]);
            } else {
                log.appendChild(el);
            }
        }

        bridge.init(function(message, responseCallback) {
            responseCallback({'step':'init'});
        });

        bridge.registerHandler('refreshWkey', function(data, responseCallback) {
            log(data);
            var responseData = { 'Javascript Says':'Right back atcha!' };
            responseCallback(responseData);
        })
    });
}

/**
 * IOS驱动
 */
function connectWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        window.openapk = WebViewJavascriptBridge;
        callback(WebViewJavascriptBridge);
    } else {
        document.addEventListener('WebViewJavascriptBridgeReady', function(event) {
            window.openapk = event.bridge;
            callback(WebViewJavascriptBridge);
        }, false);
    }
}

/**
 * 打开登录页
 */
function openLoginPage(){
    openapk.callHandler('openLoginPage',{a:1},function(n){
    });
}

/**
 * 打开个人中心
 * @param uid
 */
function openUserPage(uid){
    openapk.callHandler('openUserPage',{uid:uid},function(n){
    });
}

/**
 * 打开消息对话框
 * @param uid
 */
function openMessagePage(uid,username){
    openapk.callHandler('openMessagePage',{uid:uid,username:username},function(n){
    });
}

/**
 * 打开圈子
 * @param qid
 * @param styleid
 */
function openQuanPage(qid, styleid){
    openapk.callHandler('openQuanPage',{qid:qid,styleid:styleid},function(n){
    });
}

/**
 * 打开帖子
 * @param tid
 */
function openThreadPage(tid){
    openapk.callHandler('openThreadPage',{tid:tid},function(n){
    });
}

/**
 * 打开卡页面
 * @param cid
 */
function openCardItemPage(cid){
    openapk.callHandler('openCardItemPage',{cid:cid},function(n){
    });
}

function getWkey() {
    openapk.callHandler('getWkey',{a:1},function(n){
        alert(n);
    });
}